﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Serialization;

namespace Ending.Cases
{
    /// <summary>
    /// CaseBasicFunctions.xaml 的交互逻辑
    /// </summary>
    public partial class CaseBasicFunctions : Window
    {
        public CaseBasicFunctions()
        {
            InitializeComponent();
        }
        #region 
        /// <summary>
        /// 将xml文本转化为类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="strXML"></param>
        /// <returns></returns>
        public static T DESerializer<T>(string strXML) where T : class
        {
            try
            {
                using (StringReader sr = new StringReader(strXML))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    return serializer.Deserialize(sr) as T;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        /// <summary>
        /// 类对象转化为xml文本
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string XmlSerialize<T>(T obj)
        {
            using (StringWriter sw = new StringWriter())
            {
                Type t = obj.GetType();
                XmlSerializer serializer = new XmlSerializer(obj.GetType());
                serializer.Serialize(sw, obj);
                sw.Close();
                return sw.ToString();
            }
        }

        #endregion

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //文件操作
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TestXMLToObject_Click(object sender, RoutedEventArgs e)
        {
            TextRange textRange = new TextRange(rtb01.Document.ContentStart, rtb01.Document.ContentEnd);
           
            string _xml = textRange.Text;
            Classes.XML990.Document _document= DESerializer<Classes.XML990.Document>(_xml);

            rtb02.AppendText(_document.ComuCnfm.MsgRefId);

            rtb02.AppendText(XmlSerialize(_document));



        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

        }
    }
}
